﻿AB.namespace("INV_ReceiveOrder.Mgr");
INV_ReceiveOrder.Mgr = {
    commandHandler: function (command, record) {
        var FId = record.data.FId;
        switch (command) {
            case "Edit":
                FIdH.setValue(FId);
                ReceiveOrderEidtStore.load();
                EditReceiveOrderDetailStore.load();
                break;
            case "Delete":
                Ext.Msg.confirm('提示', '确定要删除该记录吗?', function (btn) {
                    if (btn == "yes") {
                        Ext.Ajax.request({
                            url: "/ReceiveOrder/DeleteReceiveOrder",
                            method: 'post',
                            params: {
                                FId: FId
                            },
                            success: function (result) {
                                if (!isTimeOut(result)) {
                                    var response = Ext.util.JSON.decode(result.responseText);
                                    if (response.success == false) {
                                        ajaxFailureHandler(response);
                                    } else {
                                        FIdH.setValue("");
                                        EditFormPanel.form.reset();

                                        var mark = OrderStatusStore.data.items;
                                        FStatus.setValue(mark[0].data.FID);

                                        EditReceiveOrderDetailStore.removeAll();
                                        ReceiveOrderListStore.reload();
                                    }
                                }
                            }
                        });
                    }
                });
                break;
        }
    },
    successHandler: function (form, action) {
        //successHandler(form, action, this.successCallback);
    },
    failureHandler: function (form, action, failureCallback) {
        failureHandler(form, action);
    },
    successCallback: function () {
    },
    failureCallback: function () {
    },
    formStoreLoaded: function (store, records) {
        var formObj = Ext.getCmp("EditFormPanel");
        formStoreLoaded(store, records, formObj);
        OrderStatusStore.load();
    },
    searchList: function () {
        ReceiveOrderListStore.reload();
    },
    saveReceiveMsg: function () {
        if (FBillDate.getValue() == "") {
            Ext.Msg.show({
                title: '操作失败',
                msg: '请选择开单日期!',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }

        var FIdT = FIdH.getValue();
        var FNumberT = FNumber.getValue();
        var FBillDateT = Ext.util.Format.date(FBillDate.getValue(), 'Y-m-d');
        var FStatusT = FStatus.getValue();
        var FRemarkT = FRemark.getValue();

        var FDetails = "";
        var view = ReceiveOrderDetailGrid.getView().ds;
        if (view != null) {
            var errorI = "";

            var mark = view.data.items;
            for (var i = 0; i < mark.length; ++i) {
                var FProductId = mark[i].data.FProductId;
                if (FProductId == "") {
                    errorI += (i + 1) + ",";
                }

                if (i == mark.length - 1) {
                    FDetails += "{FId:'" + mark[i].data.FId + "',FProductId:'" + mark[i].data.FProductId + "',FNumber:" + mark[i].data.FNumber + ",FPrice:" + mark[i].data.FPrice + ",FAmount:" + mark[i].data.FAmount + ",FRemark:'" + mark[i].data.FRemark + "'}";
                }
                else {
                    FDetails += "{FId:'" + mark[i].data.FId + "',FProductId:'" + mark[i].data.FProductId + "',FNumber:" + mark[i].data.FNumber + ",FPrice:" + mark[i].data.FPrice + ",FAmount:" + mark[i].data.FAmount + ",FRemark:'" + mark[i].data.FRemark + "'},";
                }
            }

            if (errorI != "") {
                errorI = errorI.substr(0, errorI.length - 1);
                showDetailMsg(MsgDF, "第[" + errorI + "]行产品编号无效！");
                return;
            }
        }

        var msg = "{FId:'" + FIdT + "',FNumber:'" + FNumberT + "',FBillDate:'" + FBillDateT + "',FStatus:'" + FStatusT + "',FRemark:'" + FRemarkT + "',FDetails:[" + FDetails + "]}";

        Ext.Ajax.request({
            url: "/ReceiveOrder/SaveReceiveOrderMsg",
            method: 'post',
            params: {
                msg: msg
            },
            success: function (result) {
                if (!isTimeOut(result)) {
                    var response = Ext.util.JSON.decode(result.responseText);
                    if (response.success == false) {
                        ajaxFailureHandler(response);
                    } else {
                        FIdH.setValue(response.result);
                        ReceiveOrderEidtStore.reload();
                        EditReceiveOrderDetailStore.reload();
                        ReceiveOrderListStore.reload();
                    }
                }
            }
        });
    },
    startEditing: function (e) {
        if (e.getKey() === e.ENTER) {
            var grid = ReceiveOrderDetailGrid;
            record = grid.getSelectionModel().getSelected();
            index = grid.store.indexOf(record);

            grid.startEditing(index, 1);
        }
    },
    afterEdit: function (e) {
        var grid = ReceiveOrderDetailGrid;
        var field = e.field;
        var valueOld = e.originalValue;
        var valueNew = e.value;
        var currentRow = e.row;
        var data = e.record.data;

        if (valueOld != valueNew) {
            if (field == "FProductNumber") {
                Ext.Ajax.request({
                    url: "/Product/GetProductMsgByFNumber",
                    method: 'post',
                    params: {
                        FNumber: valueNew,
                        priceType: ""
                    },
                    success: function (result) {
                        if (!isTimeOut(result)) {
                            var response = Ext.util.JSON.decode(result.responseText);
                            if (response.success == false) {
                                ajaxFailureHandler(response);
                            } else {
                                if (response.extraParamsResponse.Result == "true") {
                                    grid.store.getAt(currentRow).set("FProductId", response.extraParamsResponse.FId);
                                    grid.store.getAt(currentRow).set("FProductName", response.extraParamsResponse.FName);
                                    showDetailMsg(MsgDF, "");
                                } else {
                                    ReceiveOrderDetailGrid.startEditing(currentRow, 0);
                                    grid.store.getAt(currentRow).set("FProductId", "");
                                    grid.store.getAt(currentRow).set("FProductName", "");
                                    showDetailMsg(MsgDF, "输入产品编号无效！");
                                }
                            }
                        }
                    },
                    failure: failureHandler
                });
            }
            else if (field == "FNumber") {
                var amount = valueNew * data.FPrice;
                grid.store.getAt(currentRow).set("FAmount", amount);
            }
            else if (field == "FPrice") {
                var amount = valueNew * data.FNumber;
                grid.store.getAt(currentRow).set("FAmount", amount);
            }
        }
    },
    deleteReceiveOrder: function () {
        var FIdT = FIdH.getValue();
        if (FIdT == "") {
            return;
        }
        Ext.Msg.confirm('提示', '确定要删除该记录吗?', function (btn) {
            if (btn == "yes") {
                Ext.Ajax.request({
                    url: "/ReceiveOrder/DeleteReceiveOrder",
                    method: 'post',
                    params: {
                        FId: FIdT
                    },
                    success: function (result) {
                        if (!isTimeOut(result)) {
                            var response = Ext.util.JSON.decode(result.responseText);
                            if (response.success == false) {
                                ajaxFailureHandler(response);
                            } else {
                                FIdH.setValue("");
                                EditFormPanel.form.reset();

                                var mark = OrderStatusStore.data.items;
                                FStatus.setValue(mark[0].data.FID);

                                EditReceiveOrderDetailStore.removeAll();
                                ReceiveOrderListStore.reload();
                            }
                        }
                    }
                });
            }
        });
    },
    resetEditData: function () {
        FIdH.setValue("");
        EditFormPanel.form.reset();

        FBillDate.setValue(Ext.util.Format.date(new Date(), "Y/m/d"));

        var mark = OrderStatusStore.data.items;
        FStatus.setValue(mark[0].data.FID);

        EditReceiveOrderDetailStore.removeAll();
    },
    resetSearchData: function () {
        SearchFormPanel.form.reset();
    },
    approveReceiveOrder: function () {
        var FIdT = FIdH.getValue();
        if (FIdT == "") {
            return;
        }
        Ext.Msg.confirm('提示', '确定要审核该收货单吗?', function (btn) {
            if (btn == "yes") {
                Ext.Ajax.request({
                    url: "/ReceiveOrder/ApproveReceiveOrder",
                    method: 'post',
                    params: {
                        FId: FIdT
                    },
                    success: function (result) {
                        if (!isTimeOut(result)) {
                            var response = Ext.util.JSON.decode(result.responseText);
                            if (response.success == false) {
                                ajaxFailureHandler(response);
                            } else {
                                ReceiveOrderEidtStore.reload();
                                EditReceiveOrderDetailStore.reload();
                                ReceiveOrderListStore.reload();
                            }
                        }
                    }
                });
            }
        });
    },
    unApproveReceiveOrder: function () {
        var FIdT = FIdH.getValue();
        if (FIdT == "") {
            return;
        }
        Ext.Msg.confirm('提示', '确定要反审核该收货单吗?', function (btn) {
            if (btn == "yes") {
                Ext.Ajax.request({
                    url: "/ReceiveOrder/UnApproveReceiveOrder",
                    method: 'post',
                    params: {
                        FId: FIdT
                    },
                    success: function (result) {
                        if (!isTimeOut(result)) {
                            var response = Ext.util.JSON.decode(result.responseText);
                            if (response.success == false) {
                                ajaxFailureHandler(response);
                            } else {
                                ReceiveOrderEidtStore.reload();
                                EditReceiveOrderDetailStore.reload();
                                ReceiveOrderListStore.reload();
                            }
                        }
                    }
                });
            }
        });
    },
    AddColumn: function () {
        var rowIndex = ReceiveOrderDetailGrid.addRecord();
        ReceiveOrderDetailGrid.getView().focusRow(rowIndex);
        ReceiveOrderDetailGrid.startEditing(rowIndex, 0);
    },
    PrintBarcode: function () {
        var FIdT = FIdH.getValue();
        if (FIdT == "") {
            return;
        }
        window.open("/ReceiveOrder/PrintBarcode?FId=" + FIdT);
    }
}

var showDetailMsg = function (item,msg) {
    item.setValue(msg);
}